<?php
    namespace Admin\Controller;

    use Org\Util\ArrayList;
    use Org\Util\Date;
    use Think\Controller;

    class TableController extends Controller
    {
        public function index()
        {
            $pagesize = 5;#每页数量
            $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
            $count = count(M('table')->select());
            $offset = $pagesize * ($p - 1);//计算记录偏移量
            $table = M('table')
                ->limit($offset . ',' . $pagesize)
                ->select();
            $page = new \Think\Page($count, $pagesize);
            $page = $page->show();
            $this->assign('page', $page);
            $this->assign('table',$table);
            $this->display('showTable');
        }

        public function addTable(){
            $this->display();

        }
        public function updateInfo(){
            $table_id = I('table_id');
            $this->assign('table',M('table')->find($table_id));
            $this->display('addTable');
        }
        public function update(){
            $table_num = I('table_num');
            $table_id = I('table_id');
            if (!is_numeric($table_num)){
                $this->error('输入桌号不能为字符串');
            }
            $data['table_num'] = $table_num;
            $data['table_status'] = 0;

            $table = M('table');


            if ($table_id==null){
                if ($table->where('table_num='.$table_num)->find()){
                    $this->error('输入桌号已经存在');

                }
                if ($table->add($data)) {
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            }else{
                if ($table->where('table_id='.$table_id)->save($data)) {
                    $this->success('修改成功');
                }else{
                    $this->error('修改失败',U('Table/index'));
                }
            }
        }

        public function delete(){
            $table_id = I('table_id');
            $table = M('table');
            if ($table->delete($table_id)) {
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }



    }